package com.binguo.api.controller;


import com.binguo.api.form.*;
import com.binguo.api.result.Result;
import com.binguo.api.result.ResultGenerator;

import com.binguo.api.service.ApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 云澎开放平台1期
 */
@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;
    @Autowired
    private PlatformTransactionManager txManager;

    @PostMapping(value = "/api/binguo/pay")
    public Result apiPay(ApiForm<ApiPayForm> apiPayForm, String url)throws IOException {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus statu = txManager.getTransaction(def);
        try {
            apiService.pay(apiPayForm, url);
        }catch (Exception e){
            txManager.rollback(statu);
            return ResultGenerator.genFailResult(e.getMessage());
        }
        txManager.commit(statu);

        return ResultGenerator.genSuccessResult();
    }


    @PostMapping(value = "/api/binguo/product")
    public Result product(@RequestBody ApiForm<ApiProductForm> apiPayForm) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus statu = txManager.getTransaction(def);
        Result rs = new Result();
        try {
            rs = apiService.product(apiPayForm);
        }catch (Exception e){
            txManager.rollback(statu);
            return ResultGenerator.genFailResult(e.getMessage());
        }
        txManager.commit(statu);
        return rs;
    }

    @PostMapping(value = "/api/binguo/customer")
    public Result customer(@RequestBody ApiForm<ApiCustomerForm> apiForm) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus statu = txManager.getTransaction(def);
        Result rs = new Result();
        try {
            rs = apiService.customer(apiForm);
        }catch (Exception e){
            txManager.rollback(statu);
            return ResultGenerator.genFailResult(e.getMessage());
        }
        txManager.commit(statu);
        return rs;
    }

    @PostMapping(value = "/api/binguo/order")
    public Result order(@RequestBody ApiForm<ApiOrderForm> apiForm) {
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus statu = txManager.getTransaction(def);
        Result rs = new Result();
        try {
            rs = apiService.order(apiForm);
        }catch (Exception e){
            txManager.rollback(statu);
            return ResultGenerator.genFailResult(e.getMessage());
        }
        txManager.commit(statu);
        return rs;
    }

}
